﻿using System.Text;
using System.Diagnostics;
using System;


namespace ChangeListUtility
{
    class CmdExecuter
    {
        /// <summary>
        /// without redirect input/output/stderr
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public static bool executeCmd(string cmd)
        {
            Process p = new Process();
            char[] spliter = { ' ', };
            string[] spiltedCmd = cmd.Split(spliter, 2); // two strings
            p.StartInfo.FileName = spiltedCmd[0];
            p.StartInfo.Arguments = spiltedCmd[1];
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

            p.StartInfo.UseShellExecute = true;

            p.Start();

            p.WaitForExit();

            return true;
        }


        /// <summary>
        /// 
        /// </summary>
        static StringBuilder sbErrorOutput;
        private static void errorOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (!string.IsNullOrEmpty(outLine.Data) && sbErrorOutput != null)
            {
                sbErrorOutput.Append(outLine.Data);
                sbErrorOutput.Append("\r\n");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        static StringBuilder sbStdOutput;
        private static void stdOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (!string.IsNullOrEmpty(outLine.Data) && sbStdOutput != null)
            {
                sbStdOutput.Append(outLine.Data);
                sbStdOutput.Append("\r\n");
            }
        }

        public static bool executeCmd(string cmd, out string output, out string outputError)
        {
            sbErrorOutput = new StringBuilder();
            sbStdOutput = new StringBuilder();
            char[] spliter = { ' ', };
            string[] spiltedCmd = cmd.Split(spliter, 2); // two strings

            Process p = new Process();
            p.StartInfo.FileName = spiltedCmd[0];
            p.StartInfo.Arguments = spiltedCmd[1];
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            p.StartInfo.UseShellExecute = false;

            p.StartInfo.RedirectStandardError = true;
            sbErrorOutput = new StringBuilder();
            p.ErrorDataReceived += errorOutputHandler;

            p.StartInfo.RedirectStandardOutput = true;
            sbStdOutput = new StringBuilder();
            p.OutputDataReceived += stdOutputHandler;

            p.Start();

            p.BeginErrorReadLine();
            p.BeginOutputReadLine();

            p.WaitForExit();

            outputError = sbErrorOutput.ToString();
            sbErrorOutput.Clear();

            output = sbStdOutput.ToString();            
            sbStdOutput.Clear();

            return true;
        }
    }
}
